from django.contrib.auth.models import User
from django.db import models
from utils.modelsMixin import ModelSetMixin, DataTimeModelMixin

# 商品分类模型类
class Classification(ModelSetMixin):
    name = models.CharField(max_length=40, verbose_name='分类名')
    # 自己关联自己，级联删除时为空
    parent = models.ForeignKey('self', on_delete=models.SET_NULL, null=True, blank=True, verbose_name='上级分类')

    class Meta:
        db_table = 'classification'  # 表名
        verbose_name = '商品分类'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f'%s.%s' % (self.id, self.name)


# 商品模型类
class Commodity(ModelSetMixin):
    STATUS_CHOICES = (
        (0, '未发布'),
        (1, '发布')
    )
    name = models.CharField(max_length=40, verbose_name='商品名')
    caption = models.CharField(max_length=140, verbose_name='副标题')
    brand = models.CharField(max_length=40, verbose_name='品牌', null=True, blank=True)
    # 十进制数字，指定该字段可以存储的最大位数（包括整数部分和小数部分），小数部分有2位
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='单价')
    stock = models.IntegerField(verbose_name='库存')
    pack = models.TextField(verbose_name='包装信息', null=True, blank=True)
    serviceafter_sale = models.TextField(verbose_name='售后服务', null=True, blank=True)
    sales = models.IntegerField(verbose_name='销量', default=0)
    comments = models.IntegerField(default=0, verbose_name='评价数')
    status = models.IntegerField(default=0, verbose_name='状态', choices=STATUS_CHOICES)
    detail = models.TextField(verbose_name='详情', null=True, blank=True)

    # 一对多联系，每类商品有多个
    classification1 = models.ForeignKey(Classification, on_delete=models.PROTECT, verbose_name='一级分类',
                                        related_name='classification1')

    classification2 = models.ForeignKey(Classification, on_delete=models.PROTECT, verbose_name='二级分类',
                                        related_name='classification2')

    class Meta:
        # 排序：销量，评价，创建时间，从高到低
        ordering = ['-sales', '-comments', '-create_time']
        db_table = 'commodity'  # 表名
        verbose_name = '商品'
        verbose_name_plural = verbose_name


# 商品图片模型类
class CommodityImg(models.Model):
    src = models.TextField(verbose_name='图片地址')
    priority = models.IntegerField(default=0, verbose_name='优先级')
    commodity = models.ForeignKey(Commodity, on_delete=models.CASCADE, verbose_name='商品')

    class Meta:
        # 排序：按照充钱的优先级排序
        ordering = ['-priority', '-id']
        db_table = 'commodity_img'
        verbose_name = '商品图'
        verbose_name_plural = verbose_name


# 购物车的模型类
class ShoppingCart(DataTimeModelMixin):
    number = models.IntegerField(default=1, verbose_name='数量')  # 商品数量，默认1
    # 一对多，关联商品和用户
    commodity = models.ForeignKey(Commodity, on_delete=models.CASCADE, verbose_name='商品')
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户')

    class Meta:
        # 排序
        ordering = ['-update_time']
        db_table = 'shopping_cart'  # 表名
        verbose_name = '购物车'
        verbose_name_plural = verbose_name


# 订单模型类
class Order(DataTimeModelMixin):
    PAY_METHOD_CHOICES = (
        (1, '支付宝'),
        (2, '货到付款')
    )
    STATUS_CHOICES = (
        (1, '待支付'),
        (2, '待发货'),
        (3, '待收货'),
        (4, '待评价'),
        (5, '已完成'),
        (6, '已取消'),
        (7, '申请退款'),
        (8, '退款成功'),
    )

    # 把订单号作为主键
    order_id = models.CharField(max_length=64, primary_key=True, verbose_name='订单号')
    total_count = models.IntegerField(default=1, verbose_name='商品数')
    # 精确数类型
    total_amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='商品总金额')
    # 支付方式默认第一种，使用支付宝
    pay_method = models.IntegerField(verbose_name='支付方式', default=1, choices=PAY_METHOD_CHOICES)
    # 状态默认第一种，待支付状态
    status = models.IntegerField(verbose_name='订单状态', default=1, choices=STATUS_CHOICES)
    address = models.TextField(verbose_name='收货地址')
    # 一对多，关联用户，不可被删除
    user = models.ForeignKey(User, on_delete=models.PROTECT, verbose_name='用户')

    class Meta:
        # 排序
        ordering = ['-create_time']
        db_table = 'order'  # 表名
        verbose_name = '订单表'
        verbose_name_plural = verbose_name


# 订单详情模型类 ，购买后针对于原订单商品数的减少，单价的修改，评价及评分
class OrderGoods(DataTimeModelMixin):
    SCORE_CHOICES = (
        (0, '零星'),
        (1, '一星'),
        (2, '两星'),
        (3, '三星'),
        (4, '四星'),
        (5, '五星'),
    )
    number = models.IntegerField(default=1, verbose_name='商品数')
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='单价')
    comment = models.TextField(verbose_name='评价', null=True, blank=True)
    score = models.IntegerField(null=True, blank=True, verbose_name='评分', choices=SCORE_CHOICES)
    # 选择匿名评价不展示用户名
    is_anonymous = models.BooleanField(default=False, verbose_name='匿名评价')
    # 一对多，关联订单
    order = models.ForeignKey(Order, on_delete=models.CASCADE, verbose_name='订单')
    # 一对多，关联商品
    commodity = models.ForeignKey(Commodity, on_delete=models.SET_NULL, null=True, blank=True, verbose_name='商品')

    class Meta:
        db_table = 'order_goods'  # 表名
        verbose_name = '订单商品'
        verbose_name_plural = verbose_name


# 支付宝凭证模型类
class Payment(DataTimeModelMixin):
    trade_id = models.CharField(max_length=100,unique=True,verbose_name='支付交易号')
    order = models.ForeignKey(Order,on_delete=models.CASCADE,verbose_name='订单')

    class Meta:
        # 排序
        ordering = ['-create_time']
        db_table = 'payment'  # 表名
        verbose_name = '支付信息'
        verbose_name_plural = verbose_name
